package com.gy.paidao;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.example.mapdemo.MainActivity;
import com.example.mapdemo.R;
import com.gy.paidao.bean.BaseBean;
import com.gy.paidao.main.MapSearchActivity;
import com.gy.paidao.person.LoginActivity;
import com.gy.paidao.person.bean.LoginBean;
import com.gy.paidao.util.AnimUtil;
import com.gy.paidao.util.LogUtil;
import com.gy.paidao.util.ParseBase;
import com.gy.paidao.util.SPHelper;
import com.gy.paidao.util.Urls;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;

/**
 * 作者：gy on 16/2/20
 * 说明：
 */
public class LoadingActivity extends BaseActivity {

    private Class intoActivity;
    private String userLoginID;
    private boolean requestFlag = false;//判断网络是否请求结束
    private boolean timeOut = false;//判断3秒是否超时

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading_layout);
        userLoginID = SPHelper.getInstance(this).getStringData("userLoginID", null);
        if (TextUtils.isEmpty(userLoginID)) {
            intoActivity = LoginActivity.class;
            requestFlag = true;
        } else {
            intoActivity = MainActivity.class;
            dorequestLogin();
        }
        handler.sendEmptyMessageDelayed(0, 500);
    }

    @Override
    protected void initVariable() {

    }

    @Override
    protected void setTitleViews() {

    }

    @Override
    protected void setViews() {

    }

    private void dorequestLogin() {
        final OkHttpClient client = new OkHttpClient();
        client.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
        final Request request = new Request.Builder()
                .url(Urls.url_queryloginstatus)
                .addHeader("Cookie", "userLoginID=" + userLoginID)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                requestFlag = true;
                intoActivity = LoginActivity.class;
                if (timeOut) {
                    Intent intent = new Intent(LoadingActivity.this, intoActivity);
                    startActivity(intent);
                    AnimUtil.pushLeftInAndOut(LoadingActivity.this);
                    LoadingActivity.this.finish();
                }
            }

            @Override
            public void onResponse(final Response response) throws IOException {
                requestFlag = true;
                dismissLoading();
                BaseBean baseBean = ParseBase.parseBase(response);
                if (baseBean.success) {
                    String data = String.valueOf(baseBean.data);
                    LoginBean bean = gson.fromJson(data, LoginBean.class);
                    MyApplication.loginBean = bean;
                    MyApplication.USERID = bean.id;
                    intoActivity = MainActivity.class;
                } else {
                    intoActivity = LoginActivity.class;
                }
                if (timeOut) {
                    Intent intent = new Intent(LoadingActivity.this, intoActivity);
                    startActivity(intent);
                    AnimUtil.pushLeftInAndOut(LoadingActivity.this);
                    LoadingActivity.this.finish();
                }
            }
        });

    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            timeOut = true;
            if (requestFlag) {
                Intent intent = new Intent(LoadingActivity.this, intoActivity);
                startActivity(intent);
                AnimUtil.pushLeftInAndOut(LoadingActivity.this);
                LoadingActivity.this.finish();
            }
        }
    };
}
